﻿using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SBPweb.RIA.Shared.Contracts.Events
{

    public class DynamicContextRequestQueue
    {

        private Queue<DynamicContextRequest> requestQueue;
        private Dictionary<DynamicContextRequest, object> requestResults;
        private DynamicContextRequest lastRequest;

        public DynamicContextRequestQueue()
        {
            requestQueue = new Queue<DynamicContextRequest>();
            requestResults = new Dictionary<DynamicContextRequest, object>();
        }

        public void Enqueue(DynamicContextRequest request)
        {
            requestQueue.Enqueue(request);
        }

        internal IDictionary<DynamicContextRequest, object> Results
        {
            get
            {
                return requestResults;
            }
        }

        public void Dequeue(object result)
        {
            if (lastRequest != null)
            {
                requestResults.Add(lastRequest, result);
            }
            if (requestQueue.Count == 0)
            {
                return;
            }
            lastRequest = requestQueue.Dequeue();
            lastRequest.EvaluateResult(this);
        }

        public void Dequeue()
        {
            lastRequest = null;
            Dequeue(null);
        }
        
    }

}